﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CluelessServer
{
    public class PlayerList
    {
        private LinkedList<Player> players;
        private int currentPlayer;


        public PlayerList()
        {
            players = new LinkedList<Player>();
            currentPlayer = 0;
        }
    
        public LinkedList<Player> Players
        {
            get
            {
                return players;
            }
            set
            {
                players = value;
            }
        }

        /// <summary>
        /// Adds a player to the game
        /// </summary>
        /// <param name="p"></param>
        public void AddPlayer(Player p)
        {
            players.AddLast(p);
        }

        /// <summary>
        /// Removes a player from the list of players
        /// </summary>
        /// <param name="p"></param>
        public void RemoverPlayer(Player p)
        {
            players.Remove(p);
        }

        /// <summary>
        /// Returns the current player
        /// </summary>
        /// <returns></returns>
        public Player GetCurrentPlayer()
        {
            return players.ElementAt(currentPlayer);
        }

        /// <summary>
        /// Determines and returns the next player
        /// </summary>
        /// <returns></returns>
        public Player GetNextPlayer()
        {
            if (currentPlayer == players.Count - 1)
            {
                currentPlayer = 0;
                return GetCurrentPlayer();
            }
            else
            {
                currentPlayer += 1;
                return GetCurrentPlayer();
            }
        }

        /// <summary>
        /// Used to return the number of players in the game
        /// </summary>
        public int NumberOfPlayers
        {
            get
            {
                return Players.Count;
            }
            set
            {
            }
        }

        /// <summary>
        /// Finds the user with the given name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Player GetPlayer(String name)
        {
            foreach (Player p in players)
            {
                //if (p.Name == name)
                if(p.Character.Name == name)
                {
                    return p;
                }
            }

            throw new Exception("Failed to find the player");
        }

    }
}
